package com.adcc.ags.msgStore.healthcheck;

import com.adcc.ags.msgStore.entity.mq.MQInfo;
import com.adcc.ags.msgStore.entity.mq.MQState;
import com.adcc.ags.msgStore.managed.MessageForward;
import com.adcc.utility.log.Log;
import com.codahale.metrics.health.HealthCheck;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * MQ健康检查
 */
public class MQHealthCheck extends HealthCheck {

    @Override
    protected Result check() throws Exception {
        try {
            MQInfo mqInfo = MessageForward.getInstance().getMQInfo();
            if (mqInfo.getState() == MQState.CONNECTED) {
                return Result.healthy(new ObjectMapper().writeValueAsString(mqInfo));
            } else {
                return Result.unhealthy(new ObjectMapper().writeValueAsString(mqInfo));
            }
        } catch (Exception ex) {
            Log.error(MQHealthCheck.class.getName(), "check() error", ex);
            return Result.unhealthy(ex.toString());
        }
    }
}
